package com.emergency.db.service.impl.staff;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.staff.StaffStatusDao;
import com.emergency.db.domain.staff.Staff;
import com.emergency.db.domain.staff.StaffStatus;
import com.emergency.db.service.staff.StaffStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StaffStatusServiceImpl implements StaffStatusService {
    private final static Logger logger = LoggerFactory.getLogger(StaffStatusServiceImpl.class);

    @Autowired
    private StaffStatusDao staffStatusDao;

    @Autowired
    private IdWorker idWorker;

    public Long save(StaffStatus staffStatus) {
        if (CommonUtils.isNullOrEmpty(staffStatus.getId())) {
            staffStatus.setId(idWorker.nextId());
            staffStatus.setCreateTime(new Date());
        }
        //保存状态信息
        staffStatusDao.save(staffStatus);
        return staffStatus.getId();
    }

    //查询单项
    public StaffStatus findById(Long id) {
        StaffStatus staffStatus = staffStatusDao.findById(id).orElse(null);
        if (CommonUtils.isNullOrEmpty(staffStatus)) {
            logger.info("此状态信息为空！");
            return staffStatus;
        }

        return staffStatus;
    }

    //根据staff表查询
    public List<StaffStatus> findAllByStaff(Long id) {
        List<StaffStatus> staffStatusList = staffStatusDao.findAllByStaff(id);
        if (CommonUtils.isNullOrEmpty(staffStatusList)) {
            logger.info("此人状态信息为空");
            return staffStatusList;
        }
        return staffStatusList;
    }

    //删除
    public void delete(Long id) {
        staffStatusDao.deleteById(id);
    }

    //删除ByStaff
    public void deleteAllByStaff(Long id) {
        staffStatusDao.deleteAllByStaff(id);
    }

    //分页查询
    @Override
    public Page<StaffStatus> findList(Map searchMap, int page, int size) {
        Specification<StaffStatus> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return staffStatusDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<StaffStatus> createSpecification(Map searchMap) {
        return new Specification<StaffStatus>() {

            @Override
            public Predicate toPredicate(Root<StaffStatus> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 名称
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                // 所当前任务
                if (searchMap.get("task") != null && !"".equals(searchMap.get("task"))) {
                    predicateList.add(criteriaBuilder.like(root.get("task").as(String.class), "%" + (String) searchMap.get("task") + "%"));
                }
                // 持续工作时间
                if (searchMap.get("workTime") != null && !"".equals(searchMap.get("workTime"))) {
                    Long wt = ((Number) searchMap.get("workTime")).longValue();//long类型在json下格式转换异常
                    predicateList.add(criteriaBuilder.ge(root.get("workTime").as(Long.class), wt));
                }
                //累计剂量
                if (searchMap.get("dose") != null && !"".equals(searchMap.get("dose"))) {
                    predicateList.add(criteriaBuilder.ge(root.get("dose").as(double.class), (double) searchMap.get("dose")));
                }
                // 健康状态
                if (searchMap.get("health") != null && !"".equals(searchMap.get("health"))) {
                    predicateList.add(criteriaBuilder.like(root.get("health").as(String.class), (String) searchMap.get("health")));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
